// https://nuxt.com/docs/api/configuration/nuxt-config
import { defineNuxtConfig } from "nuxt/config";
// import vuetify, { transformAssetUrls } from "vite-plugin-vuetify";
import viteCompression from "vite-plugin-compression";
import importToCDN from "vite-plugin-cdn-import";
import Components from "unplugin-vue-components/vite";
import { AntDesignVueResolver } from "unplugin-vue-components/resolvers";
import { chunkSplitPlugin } from "vite-plugin-chunk-split";
import CompressionPlugin from "compression-webpack-plugin";
// import { visualizer } from "rollup-plugin-visualizer";
export default defineNuxtConfig({
  devtools: { enabled: true },
  ssr: true,
  serverMiddleware: [{ path: "/", handler: "~/server/middleware/proxy.js" }],
  runtimeConfig: {
    public: {
      server_url: "https://adminer.fly2sc.com",
    },
  },
  routeRules: {
    "/mobile/**": { ssr: false },
  },
  build: {
    cache: true,
    transpile: [],
    optimization: {
      minimize: true,
      minimizer: [
        // new CssMinimizerPlugin(),
      ],
      splitChunks: {
        // 生成 chunk 的最小体积（以 bytes 为单位）
        chunks: "all",
        automaticNameDelimiter: "-",
        maxSize: 102400,
        cacheGroups: {
          vendors: {
            test: /[\\/]node_modules[\\/]/,
            priority: -10, //优先级
            reuseExistingChunk: true,
          },
          common: {
            minChunks: 2,
            priority: -20, //优先级
            reuseExistingChunk: true,
          },
        },
      },
    },
  },

  app: {
    head: {
      // link: [
      //   {
      //     type: "text/css",
      //     href: "https://cdn.jsdelivr.net/npm/ant-design-vue@4.2.3/dist/reset.min.css",
      //   },
      // ],
      // script: [
      //   {
      //     src: "https://cdn.jsdelivr.net/npm/ant-design-vue@4.2.3/dist/antd.min.js",
      //   },
      // ],
    },
  },

  imports: {
    dirs: [
      // 扫描顶级模块
      "composables",
      // ... 或扫描带有特定名称和文件扩展名的一级嵌套模块
      "composables/*/index.{ts,js,mjs,mts}",
      // ... 或扫描给定目录中的所有模块
      "composables/**",
    ],
  },

  components: [{ path: "~/components-module/components" }, "~/components"],

  modules: [
    "nuxt-lodash",
    [
      "@nuxtjs/i18n",
      {
        vueI18n: "./i18n.config.ts",
        strategy: "no_prefix",
      },
    ],
    [
      "@pinia/nuxt",
      {
        autoImports: [
          // 自动引入 `defineStore()`
          "defineStore",
          // 自动引入 `defineStore()` 并重命名为 `definePiniaStore()`
          ["defineStore", "definePiniaStore"],
        ],
      },
    ],
    "@pinia-plugin-persistedstate/nuxt",
    "nuxt-swiper",
    "@ant-design-vue/nuxt",
  ],

  i18n: {
    locales: ["en", "cn"],
    defaultLocale: "en",
    vueI18n: "./i18n.config.ts",
  },

  vite: {
    build: {
      minify: "terser",
      terserOptions: {
        compress: {
          drop_debugger: true,
          drop_console: true,
        },
      },
      rollupOptions: {
        // external: ["ant-design-vue", "vue"],
        output: {
          manualChunks(id) {
            //静态资源分拆打包
            if (id.includes("node_modules")) {
              return id
                .toString()
                .split("node_modules/")[1]
                .split("/")[0]
                .toString();
            }
          },
        },
      },
    },
    vue: {
      template: {
        // transformAssetUrls,
      },
    },
    resolve: {
      alias: {
        "ant-design-vue/dist": "ant-design-vue/dist",
        "ant-design-vue/es": "ant-design-vue/es",
        "ant-design-vue/lib": "ant-design-vue/es",
        "ant-design-vue": "ant-design-vue/es",
      },
    },
    server: {
      proxy: {
        "/pdf_img": {
          target: "https://yindu.dllca.cn",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/pdf_img/, ""),
        },
      },
    },
    plugins: [
      chunkSplitPlugin(),
      viteCompression({
        verbose: true, //是否在控制台输出压缩结果
        disable: false, //是否禁用,相当于开关在这里
        // threshold: 10240, //体积大于 threshold 才会被压缩,单位 b，1b=8B, 1B=1024KB  那我们这里相当于 9kb多吧，就会压缩
        algorithm: "gzip", //压缩算法,可选 [ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
        ext: ".gz", //文件后缀
      }),
      // visualizer({
      //   emitFile: false,
      //   filename: "stats.html", //分析图生成的文件名
      //   open: true, //如果存在本地服务端口，将在打包后自动展示
      //   gzipSize: true,
      // }),
    ],
    // 定义部署后的端口
    define: {
      "process.env.PROT": 10000,
    },
  },

  css: ["~/assets/css/main.css"],

  postcss: {
    plugins: {
      tailwindcss: {},
      autoprefixer: {},
      "postcss-px-to-viewport-8-plugin": {
        unitToConvert: "px", // 需要转换的单位，默认为"px"
        viewportWidth: 1920, // 设计稿的视口宽度
        unitPrecision: 5, // 单位转换后保留的精度
        propList: ["*"], // 能转化为vw的属性列表
        viewportUnit: "vw", // 希望使用的视口单位
        fontViewportUnit: "vw", // 字体使用的视口单位
        selectorBlackList: [".ql-"], // 需要忽略的CSS选择器，不会转为视口单位，使用原有的px等单位。
        minPixelValue: 1, // 设置最小的转换数值，如果为1的话，只有大于1的值会被转换
        mediaQuery: true, // 媒体查询里的单位是否需要转换单位
        replace: true, //  是否直接更换属性值，而不添加备用属性
        exclude: undefined, // 忽略某些文件夹下的文件或特定文件，例如 'node_modules' 下的文件
        include: undefined, // 如果设置了include，那将只有匹配到的文件才会被转换
        landscape: false, // 是否添加根据 landscapeWidth 生成的媒体查询条件 @media (orientation: landscape)
        landscapeUnit: "vw", // 横屏时使用的单位
        landscapeWidth: 1338, // 横屏时使用的视口宽度
      },
    },
  },

  nitro: {
    alias: {
      pinia: "pinia",
    },
  },

  devServer: {
    port: 8080,
  },

  compatibilityDate: "2025-02-18",
});
